<?php
// $Id$
class PriceModel extends CommonModel {
    private $errmsg = '';

	public function getPrice($id) {
		$condition = array();
		$condition['house_id'] = $id;
		$condition['start_day'] = 0;
		$condition['end_day'] = 0;
		$default = $this->field('price')->where($condition)->find();
		unset($condition['start_day']);
		$condition['end_day'] = array('neq',0);
		$more = $this->where($condition)->field('start_day,end_day,price')->order('start_day')->select();
		return array('default'=>$default['price'],'more'=>$more);
	}
	public function addPrice($meta) {
		$house_id = intval($meta['house_id']);//得到房型的id
        //初始化默认价格
        $data = array('house_id'=>$house_id,
                'start_day'=>0,
                'end_day'=>0,
                'price'=>floatval($meta['default']),
                'create_time'=>time()
        );
        $prices = array();//初始化价格体系
        asort($meta['start_day']);
        $iszero = 0;//判断是否有样品价格
        $errflg = 0; //出错标志
        $saveflag =0; //存储标志
        $old_start_day = -1;
        $old_end_day = -1;
        $i=0;
        foreach ($meta['start_day'] as $key => $val)
        {
            //过滤起始重量小于０,起始重量大于终止重量，金额值小于0的情况
            $saveflag = ($meta['start_day'][$key]>=0) && ($meta['start_day'][$key]<$meta['end_day'][$key])  && ($meta['value'][$key]>=0);
            if (!$saveflag) continue; //如果上面的条件不满足直接继续下面一次循环
            //判断当前的区间范围是否处于上一个区间范围内
            $errflg = ($meta['start_day'][$key]>=$old_start_day  && $meta['start_day'][$key]<=$old_end_day);
            if ($errflg) break;
                $old_start_day = $meta['start_day'][$key];//存储最大重量和最小重量
                $old_end_day = $meta['end_day'][$key];
            //对样品价格区间重复的处理
            if ($meta['start_day'][$key]==0 && $meta['end_day']==0)
            {
                $iszero+=1;
            }
            if ($saveflag==1 && $iszero<=1)
            {
                    $prices[$i]['start_day']=floatval($meta['start_day'][$key]);
                    $prices[$i]['end_day'] = floatval($meta['end_day'][$key]);
                    //$prices[$i]['price'] = ($meta['start_day'][$key]==0)?0:floatval($meta['value'][$key]);
                    $prices[$i]['price'] = floatval($meta['value'][$key]);//仍是原來价格
                    $i+=1;
            }
        }
//        dump($prices);
//        dump($errflg);
        if (($meta['default']<0 && count($prices)==0) || $errflg)
        {
            $this->errmsg = '价格体系输入有误，请重新输入';
            return false;
        }
       $this->where('house_id='.$house_id)->delete();//删除指定产品的所有价格体系
       $this->add($data);
       foreach ($prices as $key => $val)
       {
           $data['start_day'] = $val['start_day'];
           $data['end_day'] = $val['end_day'];
           $data['price'] =$val['price'];
           $this->add($data);
       }
        return true;
	}
    public function getErrmsg()
    {
        return $this->errmsg;
    }

}
